package in.ac.dei.edrp.cms.common.utility;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import java.awt.Color;

import java.io.FileOutputStream;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class PdfRevenueReportView extends AbstractPdfView {
    @SuppressWarnings("unchecked")
    protected void buildPdfDocument(Map model, Document document,
        PdfWriter writer, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        List<List<String>> sheetData = (List<List<String>>) model.get(
                "revenueData");

        Table table = new Table(sheetData.get(1).size());

        for (int i = 1; i < sheetData.size(); i++) {
            for (int j = 0; j < sheetData.get(i).size(); j++) {
                table.addCell(sheetData.get(i).get(j));
            }
        }

        Paragraph title = new Paragraph("Award Sheet",
                FontFactory.getFont(FontFactory.TIMES_ROMAN, 16, Font.BOLD,
                    new Color(0, 0, 255)));

        Paragraph entity = new Paragraph("Entity : " + sheetData.get(0).get(0));
        Paragraph program = new Paragraph("Program : " +
                sheetData.get(0).get(1));
        Paragraph Branch = new Paragraph("Branch : " + sheetData.get(0).get(2));
        Paragraph Specialization = new Paragraph("Specialization : " +
                sheetData.get(0).get(3));
        Paragraph semester = new Paragraph("Semester : " +
                sheetData.get(0).get(4));
        Paragraph courseCode = new Paragraph("Course Code: " +
                sheetData.get(0).get(5));
        Paragraph courseName = new Paragraph("Course Name: " +
                sheetData.get(0).get(6));

        writer = PdfWriter.getInstance(document,
                new FileOutputStream("D:\\" +
                    sheetData.get(0).get(5) + ".pdf"));

        document.open();
        document.setPageSize(PageSize.A4.rotate());

        Table sheetHeaders = new Table(4, 4);
        sheetHeaders.setBorderColor(new Color(255, 255, 255));
        sheetHeaders.setBorderWidth(0);

        Cell c1 = new Cell("Entity : ");
        c1.setHeader(true);
        c1.setBorder(Rectangle.NO_BORDER);
        sheetHeaders.addCell(c1);
        c1 = new Cell(sheetData.get(0).get(0));
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell("Program : ");
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell(sheetData.get(0).get(1));
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell("Branch : ");
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell(sheetData.get(0).get(2));
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell("Specialization : ");
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell(sheetData.get(0).get(3));
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell("Semester : ");
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell(sheetData.get(0).get(4));
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell("Course Code: ");
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell(sheetData.get(0).get(5));
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell("Course Name: ");
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        c1 = new Cell(sheetData.get(0).get(6));
        sheetHeaders.addCell(c1);
        c1.setBorder(Rectangle.NO_BORDER);
        sheetHeaders.endHeaders();

        document.add(title);
        document.add(entity);
        document.add(program);
        document.add(Branch);
        document.add(Specialization);
        document.add(semester);
        document.add(courseCode);
        document.add(courseName);
        //		document.add(sheetHeaders);
        document.add(table);
        document.close();
    }
}
